<?php
namespace app\admin\controller;
use app\admin\model\orderModel;
use controller\BasicAdmin;
use think\Db;
class Adminorder extends BasicAdmin{
    public $table = 'order';
    public $shop = 'shop';

    public function index(){
        list($get, $db) = [$this->request->get(), Db::name($this->table)
            ->alias('a')->join('t_shop b','a.shop_id=b.id','LEFT')
            ->join('t_user c','a.user_id=c.id')
            ->field('a.tel,a.created_at,a.alipay_trade_no,a.status,a.id,a.sn,a.total_price,a.tax_price,a.service_price,a.real_price,c.username,b.name')];
        if (isset($get['created_at']) && $get['created_at'] !== '') {
            list($start, $end) = explode(' - ', $get['created_at']);
            $db->whereBetween('a.created_at', ["{$start} 00:00:00", "{$end} 23:59:59"]);
        }
        if (isset($get['sn']) && $get['sn'] !== '') {
            $db->where(['a.sn'=>trim($get['sn'])]);
        }
        if (isset($get['shop']) && $get['shop'] !== '') {
            $db->where(['b.name'=>trim($get['shop'])]);
        }
        if (isset($get['name']) && $get['name'] !== '') {
            $db->where(['c.username'=>trim($get['name'])]);
        }
        if(isset($get['type']) && $get['type'] !==''){
            $db->where(['a.status'=>trim($get['type'])]);
        }
        $list = parent::_list($db->order('created_at DESC'),1,'','');
        $orderModel = new orderModel();
        $list['list'] = $orderModel->getOrStNa($list['list']);
        $list['type'] = $orderModel->orderStatus();
//        echo'<pre>';
//        print_r($list['list']);die;
        $list['all_real_price'] = 0;//历史原价
        $list['real_price'] = 0;//当前页原价
        $list['all_otal_price'] = 0;//历史实际支付
        $list['total_price'] = 0;//当前页实际支付
        $list['all_service_price'] = 0;//历史服务费
        $list['service_price'] = 0;//当前页服务费
        $list['all_tax_price'] = 0;//历史服务费
        $list['tax_price'] = 0;//当前页服务费
        if(!empty($list['list'])){
            foreach ($list['list'] as $v){
                $list['real_price'] += $v['real_price'];
                $list['total_price'] += $v['total_price'];
                $list['service_price'] += $v['service_price'];
                $list['tax_price'] +=  $v['tax_price'];
            }

            $allwhere ='';
            if (isset($get['shop']) && $get['shop'] !== '') {
                $allwhere['b.name'] = trim($get['shop']);
            }
            if (isset($get['name']) && $get['name'] !== '') {
                $allwhere['c.username'] = trim($get['name']);
            }
            if(isset($get['type']) && $get['type'] !==''){
                $allwhere['a.status'] = trim($get['type']);
            }
            $alldb = Db::name($this->table);
            if (isset($get['created_at']) && $get['created_at'] !== '') {
                list($start, $end) = explode(' - ', $get['created_at']);
                $alldb->whereBetween('a.created_at', ["{$start} 00:00:00", "{$end} 23:59:59"]);
            }

            $allorder = $alldb->alias('a')
                ->join('t_shop b','a.shop_id=b.id','LEFT')
                ->join('t_user c','a.user_id=c.id')
                ->where($allwhere)
                ->field('a.real_price,a.total_price,a.service_price,a.tax_price')->select();

            foreach ($allorder as $value){
                $list['all_real_price'] += $value['real_price'];
                $list['all_otal_price'] += $value['total_price'];
                $list['all_service_price'] += $value['service_price'];
                $list['all_tax_price'] += $value['tax_price'];
            }
        }
        return $this->fetch('', ['title' => '订单列表','list'=>$list]);
    }

    public function edit(){
        list($get, $db) = [$this->request->get(), Db::name($this->table)->alias('a')->join('t_user b','a.user_id=b.id')->field('a.real_price,a.service_price,a.tax_price,a.total_price,a.id,a.shop_id,a.sn,a.status,a.type,a.remarks,b.nickname,b.mobile')];
        $info = $db->where(['a.id'=>$get['id']])->find();
        $info['shopNmae'] = Db::name('shop')->where(['id'=>$info['shop_id']])->value('name');
        return $this->fetch('', ['title' => '订单详情','info'=>$info]);
    }

    /**
     * 保存修改
     */
    public function postedit(){

        list($post, $db) = [$this->request->post(), Db::name($this->table)];
        $result = Db::name($this->table)->where(['id'=>$post['id']])->update(['remarks'=>$post['remarks']]);
        if($result){
           $this->success('修改成功','');
        }else{
            $this->error('修改失败','');
        }
    }

    /**
     * 查看备注
     */
    public function lookremarks(){
        return $this->_form($this->table, 'form');
    }


}